package com.smart.four.ioc.lifecycle.beanfactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;

/**
 * @author junrui.wang
 * @create 2018/5/28
 */
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
    @Override
    //接口方法，在bean实例化之前调用
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if("car".equalsIgnoreCase(beanName)){
            System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation() run");
        }
        return null;
    }

    @Override
    //接口方法，在bean实例化之后调用
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if("car".equalsIgnoreCase(beanName)){
            System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation() run");
        }
        return true;
    }

    @Override
    //接口方法，在设置某个属性是调用
    public PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
            throws BeansException {

        //仅对容器中的car bean进行处理，还可以通过pdst入参进行过滤
        //仅对car的某个特定属性值进行处理
        if("car".equalsIgnoreCase(beanName)){
            System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues() run");
        }

        return pvs;
    }
}
